//
//  QBVideoCell.m
//  QiuBai
//
//  Created by yaohongchao on 5/8/16.
//  Copyright © 2016 yaohongchao. All rights reserved.
//

#import "QBVideoCell.h"
#import "QBUserInfoView.h"
#import "QBVoteView.h"
#import "YYLabel.h"
#import "YYLabel+Night.h"

@interface QBVideoCell()
@property (nonatomic,weak) YYLabel        *lblContent;
@property (nonatomic,weak) QBUserInfoView *userView;
@property (nonatomic,weak) QBVoteView     *voteView;
@property (nonatomic,weak) UIView         *imageContent;
@property (nonatomic,weak) UIButton       *btnPlay;
@end


@implementation QBVideoCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setui];
    }
    return self;
}

-(void)setui{
    self.dk_backgroundColorPicker = DKColorPickerWithKey(BG);
    
    QBUserInfoView *userView = [[QBUserInfoView alloc]init];
    userView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:userView];
    self.userView = userView;
    userView.dk_backgroundColorPicker = DKColorPickerWithKey(CellBg);
    userView.dk_tintColorPicker = DKColorPickerWithKey(CellSelectedBg);
    
    UIView *imageContent = [[UIView alloc]init];
    imageContent.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:imageContent];
    self.imageContent = imageContent;
    imageContent.dk_backgroundColorPicker = DKColorPickerWithKey(CellBg);
    imageContent.dk_tintColorPicker = DKColorPickerWithKey(CellSelectedBg);
    
    UILabel *lblContent = [[UILabel alloc]init];
    lblContent.dk_textColorPicker = DKColorPickerWithKey(textColorCell);
    lblContent.numberOfLines = 0;
    lblContent.font = fontContentCell;
    lblContent.lineBreakMode =NSLineBreakByCharWrapping;
    [imageContent addSubview:lblContent];
    self.lblContent = lblContent;
    
    YLImageView *imgV = [[YLImageView alloc]init];
    imgV.tag = 101;
    [imageContent addSubview:imgV];
    imgV.userInteractionEnabled = YES;
    self.imgV = imgV;
    imgV.dk_backgroundColorPicker = DKColorPickerWithKey(CellBg);
    imgV.dk_tintColorPicker = DKColorPickerWithKey(CellSelectedBg);
    
    UIButton *btnPlay = [[UIButton alloc]init];
    [btnPlay setImage:[UIImage imageNamed:@"qb_video_play"] forState:UIControlStateNormal];
    [btnPlay addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [imgV addSubview:btnPlay];
    self.btnPlay = btnPlay;
    
    QBVoteView *voteView = [[QBVoteView alloc]init];
    voteView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:voteView];
    self.voteView = voteView;
    voteView.dk_backgroundColorPicker = DKColorPickerWithKey(CellBg);
    voteView.dk_tintColorPicker = DKColorPickerWithKey(CellSelectedBg);
    //设置约束
    [self makeConstraint];
}

-(void)makeConstraint{
    WS(weakSelf);
    [self.userView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(65);
    }];
    [self.imageContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.userView.mas_bottom);
        make.bottom.equalTo(weakSelf.voteView.mas_top);
        make.left.right.equalTo(weakSelf.contentView);
    }];
    
    [self.lblContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.imageContent).offset(kMargin);
        make.right.equalTo(weakSelf.imageContent).offset(-kMargin);
        make.top.mas_equalTo(weakSelf.imageContent);
        make.bottom.mas_equalTo(weakSelf.imgV.mas_top).offset(-kMargin);
    }];
    [self.imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(kMargin);
        make.right.equalTo(weakSelf.contentView).offset(-kMargin);
        make.top.mas_equalTo(weakSelf.lblContent.mas_bottom).offset(kMargin);
        make.height.mas_equalTo(0);
    }];
    
    [self.btnPlay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(weakSelf.imgV);
        make.width.height.mas_equalTo(@50);
    }];
    
    [self.voteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.contentView);
        make.bottom.equalTo(weakSelf.contentView).offset(-kMargin);
        make.top.equalTo(weakSelf.imageContent.mas_bottom);
        make.height.mas_equalTo(90);
    }];
}

-(void)play:(UIButton*)btn{
    NSLog(@"play");
    if(self.playBlock) self.playBlock(btn);
}

-(void)setItem:(Item *)item{
    _item = item;
    item.votes.comment = item.comments_count;
//    self.userView.item = item;
    self.voteView.vote = item.votes;
    self.lblContent.text = item.content;
    WS(weakSelf);
    [self.imgV sd_setImageWithURL:[NSURL URLWithString:item.pic_url]];
//    self.playerView.videoURL = [NSURL URLWithString:item.high_url];
    [self.imgV mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(kMargin);
        make.right.equalTo(weakSelf.contentView).offset(-kMargin);
        make.top.mas_equalTo(weakSelf.lblContent.mas_bottom).offset(kMargin);
        make.height.mas_equalTo(item.imageHeight);
    }];
}


@end
